package com.scoy.honeymei.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.GoodsBean;
import com.scoy.honeymei.bean.GoodsOrderBean;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.LogUtil;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: NormalAdapter$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/8/26/0026$ 下午 17:03$
 * @UpdateRemark: 更新内容
 */
public class OrderItemAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<GoodsOrderBean> datalist;
    private int ptype;

    public OrderItemAdapter(Context context, ArrayList<GoodsOrderBean> datalist, int ptype) {
        this.context = context;
        this.datalist = datalist;
        this.ptype = ptype;
    }

    public interface OnOneClick {
        void oneClick(int pos);
    }

    OnOneClick onOneClick;

    public void setOnOneClick(OnOneClick onOneClick) {
        this.onOneClick = onOneClick;
    }

    public interface OnTwoClick {
        void twoClick(int pos);
    }

    OnTwoClick onTwoClick;

    public void setOnTwoClick(OnTwoClick onTwoClick) {
        this.onTwoClick = onTwoClick;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order_item, parent, false);
        return new OrderItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        OrderItemHolder orderHolder = (OrderItemHolder) holder;
        GoodsOrderBean goodsOrderBean = datalist.get(position);
        if (ptype == 1) {
            orderHolder.cartNumcheckTv.setVisibility(View.GONE);
            orderHolder.cartBuynumTv.setVisibility(View.VISIBLE);
        }

        GlideImgUtil.glidePic(context, goodsOrderBean.getImage(), orderHolder.cartImgIv, 5);
        orderHolder.cartNameTv.setText(goodsOrderBean.getName());
        orderHolder.cartPriceTv.setText(goodsOrderBean.getMoney());
        orderHolder.ctNumTv.setText(goodsOrderBean.getNumber() + "");
        orderHolder.cartBuynumTv.setText("x" + goodsOrderBean.getNumber());

        orderHolder.ctSubtractTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onOneClick!=null){
                    onOneClick.oneClick(position);
                }

            }
        });
        orderHolder.ctAddTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onTwoClick!=null){
                    onTwoClick.twoClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return datalist == null ? 0 : datalist.size();
    }

    class OrderItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.cart_img_iv)
        ImageView cartImgIv;
        @BindView(R.id.cart_name_tv)
        TextView cartNameTv;
        @BindView(R.id.cart_pricesign_tv)
        TextView cartPricesignTv;
        @BindView(R.id.cart_price_tv)
        TextView cartPriceTv;
        @BindView(R.id.cart_buynum_tv)
        TextView cartBuynumTv;
        @BindView(R.id.ct_subtract_tv)
        TextView ctSubtractTv;
        @BindView(R.id.ct_num_tv)
        TextView ctNumTv;
        @BindView(R.id.ct_add_tv)
        TextView ctAddTv;
        @BindView(R.id.cart_numcheck_tv)
        LinearLayout cartNumcheckTv;

        OrderItemHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }



}
